/**
 * Perform the element default action.
 *
 * @properties={typeid:24,uuid:"B5A53CF6-4F44-4A4E-B119-F4C1BE8F7FA1"}
 */
function confirmar() 
{	//borrar los detalles que ya existen para esta boleta
	forms.transf_cajas.transf_cajas_to_transf_cheques.deleteAllRecords();
	
	//recorre los cheques marcados = 1 y genera los detalles todos de nuevo
	var found = databaseManager.getFoundSetCount(foundset)	
	if (found > 0)
	{
		globals.gtotal_cheques = 0;
		globals.cuantos_cheques = 0;
		for (var i = 1 ; i <= found ; i++)
		{		 
			var $rcd = foundset.getRecord(i)
			if ($rcd.marcado_cheque==1)
			{ //grabar lo que esta marcado en trans_cheques2
				forms.transf_cajas.transf_cajas_to_transf_cheques.newRecord(false)
				forms.transf_cajas.transf_cajas_to_transf_cheques.cheques_id	= $rcd.cheque_id
				forms.transf_cajas.transf_cajas_to_transf_cheques.importe		= $rcd.importe
				forms.transf_cajas.transf_cajas_to_transf_cheques.cheque_nro	= $rcd.cheque_nro
		
				globals.gtotal_cheques += $rcd.importe;
				globals.cuantos_cheques++
			}
		}
	}
	// al final todo ok hacer close del form
	forms.transf_cajas.cheques		= globals.gtotal_cheques;
	forms.transf_cajas.nro_cheques	= globals.cuantos_cheques;
	var $win = application.getWindow('chq_a_trans')
	$win.hide()
	$win.destroy()
}

/**
 * Perform the element default action.
 *
 * @properties={typeid:24,uuid:"2A95BF56-67E3-40A4-B6F5-6DCC91D33CD9"}
 */
function marcar_todo() 
{
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{		 
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_cheque = 1
	}
	recalcula_total()
}

/**
 * Perform the element default action.
 *
 * @properties={typeid:24,uuid:"1113CA23-9524-4771-BC33-6D227BC92D4E"}
 */
function desmarcar_todo()
{
	globals.gtotal_cheques = 0
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{		 
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_cheque = 0
	}
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"2FEE72D8-6F17-459F-AB08-245AB7591223"}
 * @AllowToRunInFind
 */
function buscar_chq_en_caja(firstShow, event)
{
	var $estado = foundset.find()
	if ($estado == true)
	{
		foundset.estado = globals.zconf_chq_en_caja
		foundset.cuentas_cyb_id = forms.transf_cajas.caja_origen_id

		foundset.search();
		var found = databaseManager.getFoundSetCount(foundset)
		globals.gtotal_cheques=0;
		if (found > 0)
		{
			for (var i = 1 ; i <= found ; i++)
			{
				var $rcd = foundset.getRecord(i)
				if ($rcd.marcado_cheque==1)
					globals.gtotal_cheques += $rcd.importe;
			}
		}
		else
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay cheques en caja' + forms.transf_cajas.caja_origen_id,'OK')
		}
	}
}

/**
 * Handle changed data.
 *
 * @param {Object} [oldValue] old value
 * @param {Object} [newValue] new value
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"404D06F1-93D4-4E67-AE48-FF0F1E6C05A2"}
 */
function recalcula_total(oldValue, newValue, event) 
{
	globals.gtotal_cheques=0
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
  	 {
  		var $rcd = foundset.getRecord(i)
  		if ($rcd.marcado_cheque==1)
			globals.gtotal_cheques += $rcd.importe;	 
  	 }
	return true
}
